package com.platform.api;

import com.platform.annotation.IgnoreAuth;
import com.platform.entity.*;
import com.platform.service.ApiRelatedGoodsService;
import com.platform.service.ApiTbGoodsService;
import com.platform.util.ApiBaseAction;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections.map.HashedMap;
import org.apache.log4j.Priority;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "test--商品信息接口")
@RestController
@RequestMapping("/api/tbGoods")
public class ApiTbGoodsController extends ApiBaseAction {
    @Autowired
    private ApiTbGoodsService apiTbGoodsService;

    @Autowired
    private ApiRelatedGoodsService relatedGoodsService;



    @ApiOperation(value = "商品列表")
    @IgnoreAuth
    @PostMapping("list")
    public Object list(
            @ApiParam("商品分类") @RequestParam(value = "category_id", required = false) Integer category_id,
            @ApiParam("商品名称") @RequestParam(value = "name", required = false) String name,
            @ApiParam("是否销售") @RequestParam(value = "is_sale", required = false) Integer is_sale,
            @ApiParam("是否热销") @RequestParam(value = "is_hot", required = false) Integer is_hot,
            @ApiParam("第几页") @RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
            @ApiParam("每页条数") @RequestParam(value = "pageSize", defaultValue = "100") Integer pageSize
    ) {
        List<TbGoodsVo> list = apiTbGoodsService.queryList(category_id,name,is_sale,is_hot,pageIndex,pageSize);
        return toResponsSuccess(list);
    }

    @ApiOperation(value = "商品详情")
    @IgnoreAuth
    @PostMapping("detail")
    public Object detail(@ApiParam("商品ID") @RequestParam(value = "id") Integer id) {
        Map<String,Object>  good = apiTbGoodsService.getTbGoodsVo(id);
        return toResponsSuccess(good);
    }

    @ApiOperation(value = "商品规格")
    @IgnoreAuth
    @PostMapping("spe")
    public Object spe(@ApiParam("商品ID") @RequestParam(value = "id") Integer id) {
        TbGoodsVo good=apiTbGoodsService.getTbGoodsVoById(id);
        good.setNumber(1);//设置购买商品数量，默认为1
        List<TbGoodsSpeVo> goodSpe = apiTbGoodsService.queryGoodsSpe(id);
        Map<String,Object> map=new HashedMap();
        map.put("good",good);
        map.put("goodSpe",goodSpe);
        return toResponsSuccess(map);
    }

    /**
     * 　　商品详情页的大家都在看的商品
     */
    @ApiOperation(value = "商品详情页")
    @IgnoreAuth
    @PostMapping(value = "related")
    public Object related(Integer id) {
        Map<String, Object> resultObj = new HashMap();
        List<Map<String, Object>> relatedGoods = new ArrayList();
        try{
            Map param = new HashMap();
            param.put("goods_id", id);
            param.put("fields", "related_goods_id");
            List<RelatedGoodsVo> relatedGoodsEntityList = relatedGoodsService.queryList(param);

            List<Integer> relatedGoodsIds = new ArrayList();
            for (RelatedGoodsVo relatedGoodsEntity : relatedGoodsEntityList) {
                relatedGoodsIds.add(relatedGoodsEntity.getRelated_goods_id());
            }


            if (null == relatedGoodsIds || relatedGoods.size() < 1) {
                //查找同分类下的商品
                TbGoodsVo goodsCategory = apiTbGoodsService.getTbGoodsVoById(id);
                Map paramRelated = new HashMap();
                //paramRelated.put("fields", "id, name, list_pic_url, retail_price");

                String[] catagoryIds = goodsCategory.getCategory_id().split(","); //分类id

                if(catagoryIds.length > 0) { //查找相关的产品
                    relatedGoods.addAll(apiTbGoodsService.queryRelatedList(catagoryIds, null));
                }
            } else {
                relatedGoods.addAll(apiTbGoodsService.queryRelatedList(null, relatedGoodsIds));
            }

        }catch(Exception e) {
            e.printStackTrace();
        }
        resultObj.put("goodsList", relatedGoods);
        return toResponsSuccess(resultObj);
    }



}
